package com.yixia.framework.datasource;

import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
import java.util.Map;
import javax.sql.DataSource;


/**
 * 动态数据源
 * AbstractRoutingDataSource继承该类来完成多数据源的转换
 * @author: 依梦
 * @Date 2023/8/27
 */

public class DynamicDataSource extends AbstractRoutingDataSource
{

    public DynamicDataSource(DataSource defaultTargetDataSource, Map<Object, Object> targetDataSources)
    {
        // 设置默认数据源
        super.setDefaultTargetDataSource(defaultTargetDataSource);
        super.setTargetDataSources(targetDataSources);
        super.afterPropertiesSet();
    }

    @Override
    protected Object determineCurrentLookupKey()
    {
        return DynamicDataSourceContextHolder.getDataSourceType();
    }
}